package top.lywivan.mm.service.imp;

import top.lywivan.mm.dao.CatalogDao;
import top.lywivan.mm.entity.PageResult;
import top.lywivan.mm.entity.QueryPageBean;
import top.lywivan.mm.pojo.Catalog;
import top.lywivan.mm.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    private CatalogDao catalogDao;

    @Override
    @Transactional(readOnly = true)
    public PageResult findListByPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Map queryParams = queryPageBean.getQueryParams();
        Catalog catalog = new Catalog();
        if (queryParams.get("name") != null) {
            catalog.setName("%" + queryParams.get("name") + "%");
        }
        catalog.setStatus((Integer) queryParams.get("status"));
        catalog.setCourseId((Integer) queryParams.get("courseId"));
        Long count = catalogDao.findTotalCount(catalog);
        List<Catalog> list = catalogDao.findListByPage(queryPageBean.getOffset(), pageSize, catalog);
        return new PageResult(count, list);
    }

    @Override
    @Transactional
    public void addCatalog(Catalog catalog) {
        catalogDao.addCatalog(catalog);
    }

    @Override
    @Transactional
    public void deleteCatalogById(Integer catalogId) {
        catalogDao.deleteCatalogById(catalogId);
    }
}
